package ginRoute

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

// RouteTree
type Route struct {
	path         string
	handleFunMap map[string][]gin.HandlerFunc //http方法名
	children     map[string]*Route            //children route path AND route
}

func NewRoute(path string) *Route {
	return &Route{path, make(map[string][]gin.HandlerFunc), nil}
}

func (r *Route) SetHandleFun(method string, fun ...gin.HandlerFunc) *Route {
	if r.handleFunMap == nil {
		r.handleFunMap = make(map[string][]gin.HandlerFunc)
	}
	r.handleFunMap[method] = fun
	return r
}
func (r *Route) SetFullHandleFun(fun ...gin.HandlerFunc) *Route {
	r.SetHandleFun(http.MethodGet, fun...)
	r.SetHandleFun(http.MethodPost, fun...)
	return r
}

func (r *Route) SetGetHandleFun(fun ...gin.HandlerFunc) *Route {
	r.SetHandleFun(http.MethodGet, fun...)
	return r
}
func (r *Route) SetPostHandleFun(fun ...gin.HandlerFunc) *Route {
	r.SetHandleFun(http.MethodPost, fun...)
	return r
}
func (r *Route) GetPath() string {
	return r.path
}
func (r *Route) GetChildren() map[string]*Route {
	return r.children
}
func (r *Route) GetHandleMap() map[string][]gin.HandlerFunc {
	return r.handleFunMap
}
func (r *Route) AddChildren(routes ...*Route) *Route {
	if r.children == nil {
		r.children = make(map[string]*Route)
	}
	for _, route := range routes {
		r.children[route.path] = route
	}
	return r
}
func (r *Route) DelRouteChild(routesPath ...string) {
	if r.children == nil {
		return
	}
	for i := range routesPath {
		delete(r.children, routesPath[i])
	}
}
